<template>
  <div class="borrow-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>借阅管理</span>
        </div>
      </template>
      
      <el-form :model="borrowForm" :rules="rules" ref="borrowFormRef" label-width="100px">
        <el-form-item label="图书ID" prop="bookId">
          <el-input v-model="borrowForm.bookId" placeholder="请输入图书ID" />
        </el-form-item>
        <el-form-item label="借阅人" prop="borrower">
          <el-input v-model="borrowForm.borrower" placeholder="请输入借阅人姓名" />
        </el-form-item>
        <el-form-item label="借阅日期" prop="borrowDate">
          <el-input
            v-model="borrowForm.borrowDate"
            type="date"
            placeholder="选择借阅日期"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="预计归还日期" prop="expectedReturnDate">
          <el-input
            v-model="borrowForm.expectedReturnDate"
            type="date"
            placeholder="选择预计归还日期"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleBorrow">确认借阅</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card style="margin-top: 20px">
      <template #header>
        <div class="card-header">
          <span>借阅记录</span>
        </div>
      </template>
      
      <el-table :data="borrowRecords" style="width: 100%" v-loading="loading">
        <el-table-column prop="bookId" label="图书ID" width="80" />
        <el-table-column prop="title" label="书名" />
        <el-table-column prop="borrower" label="借阅人" />
        <el-table-column prop="borrowDate" label="借阅日期">
          <template #default="scope">
            {{ formatDate(scope.row.borrowDate) }}
          </template>
        </el-table-column>
        <el-table-column prop="expectedReturnDate" label="预计归还日期">
          <template #default="scope">
            {{ formatDate(scope.row.expectedReturnDate) }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag type="warning">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import dayjs from 'dayjs'

const loading = ref(false)
const borrowFormRef = ref(null)
const borrowForm = ref({
  bookId: '',
  borrower: '',
  borrowDate: dayjs().format('YYYY-MM-DD'),
  expectedReturnDate: dayjs().add(7, 'day').format('YYYY-MM-DD')
})

const rules = {
  bookId: [{ required: true, message: '请输入图书ID', trigger: 'blur' }],
  borrower: [{ required: true, message: '请输入借阅人姓名', trigger: 'blur' }],
  borrowDate: [{ required: true, message: '请选择借阅日期', trigger: 'change' }],
  expectedReturnDate: [{ required: true, message: '请选择预计归还日期', trigger: 'change' }]
}

const borrowRecords = ref([])

const formatDate = (date) => {
  if (!date) return ''
  return date
}

const handleBorrow = async () => {
  if (!borrowFormRef.value) return
  
  await borrowFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        const formData = {
          ...borrowForm.value,
          borrowDate: formatDate(borrowForm.value.borrowDate),
          expectedReturnDate: formatDate(borrowForm.value.expectedReturnDate),
          title: '示例图书',
          status: '借阅中'
        }
        borrowRecords.value.push(formData)
        ElMessage.success('借阅成功')
        resetForm()
      } catch (error) {
        ElMessage.error('借阅失败')
      }
    }
  })
}

const resetForm = () => {
  if (borrowFormRef.value) {
    borrowFormRef.value.resetFields()
  }
}
</script>

<style scoped>
.borrow-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style> 